package cn.zhizihua.controller;


import cn.zhizihua.pojo.User;
import cn.zhizihua.query.UserQuery;
import cn.zhizihua.service.IUserService;
import cn.zhizihua.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping      //  请求方式 + 请求路径   post  /user
    public ResultBean save(@RequestBody User user){
        System.out.println("----UserController-----save--------");
        System.out.println(user);
        if (user.getUserId()==null){
            return userService.add(user);
        }
        return userService.update(user);
    }

//    @PostMapping      //  请求方式 + 请求路径   post  /user
//    public ResultBean add(@RequestBody User user){
//        System.out.println("----UserController-----add--------");
//        System.out.println(user);
//        return userService.add(user);
//    }
//
//    @PutMapping    //   put   /user
//    public ResultBean update(@RequestBody User user){
//        System.out.println("-------UserController--------update-------------");
//        System.out.println(user);
//        return userService.update(user);
//    }

    //  delete   /user/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/user/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------UserController--------del-------------");
        System.out.println(id);
        return userService.delete(id);
    }

    //  get   /user/1004
    @GetMapping("/{id}")   // http://localhost:8080/user/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------UserController--------getById-------------");
        System.out.println(id);
        return userService.getById(id);
    }

    @GetMapping("/all")    //  get  /user/all
    public ResultBean getAll(){
        System.out.println("-------UserController-----getAll--------");
        return userService.getAll();
    }

    @PostMapping("/page")   // post  /user/page
    public ResultBean getByPage(@RequestBody UserQuery userQuery){
        System.out.println("-------UserController--------getByPage-------------");
        System.out.println(userQuery);
        return userService.getByPage(userQuery);
    }

    @PostMapping("/batch/{ids}")   // post + /food/batch
    public ResultBean batchDel(@PathVariable("ids") String ids){
        System.out.println("----------FoodController---------batchDel---------------");
        System.out.println(ids); //接收到的参数格式 ： 21,19
        String[] strIds = ids.split(",");  //字符串拆分  [21,19]
        System.out.println(Arrays.toString(strIds));
        List<Integer> numids = new ArrayList<>();  //创建一个Integer集合  service层需要的参数类型
        for (String strid: strIds) {
            numids.add(Integer.parseInt(strid));//将数组中的每一项转型为Integer再加入到集合中
        }
        System.out.println(numids);
        return userService.batchDel(numids);
    }

}
